package jp.sourceforge.akjrcp.flvtools.explorer;

import java.io.File;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.ui.SimpleFileList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;

public class ConvertingMonitorDialog extends Dialog{

	private SimpleFileList fileList;

	private File files[]=new File[0];
	private int index=-1;
	private IWorkbenchSite site;
	private MonitorCancelable cancelable;
	protected ConvertingMonitorDialog(IWorkbenchSite site) {
		super(site.getShell());
		this.site=site;
	}

	public boolean isOpened(){
		return this.getShell()!=null && !this.getShell().isDisposed();
	}
protected Point getInitialSize(){
	return new Point(300,350);
	}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(Messages.getString("explorer.convertdialog"));
}
public void closeWindow(){
	this.close();
}
public void setFileList(File files[]){
	this.files=files;
}
public void updateSelect(int index){
	
	PlatformUI.getWorkbench().getDisplay().syncExec(new UpdateSelect(index));
}
public class UpdateSelect extends Thread{
	private int index;
	public UpdateSelect(int index){
		this.index=index;
	}
	public void run(){
		
		fileList.selectFile(index);
	}
}
//before open
public void selectFile(int index){
	this.index=index;
}
protected Control createDialogArea(Composite parent){
	Composite composite = (Composite)super.createDialogArea(parent);
	composite.setLayout(new GridLayout());
	fileList = new SimpleFileList(composite);
	fileList.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
	for (int i = 0; i < files.length; i++) {
		fileList.addFile(files[i]);
	}
	//fileList.addFile(new File("c:\\tmp\\"));//dummy
	fileList.setInput(site);
	fileList.selectFile(index);
	
	
	Button cancel=new Button(composite,SWT.NULL);
	cancel.addSelectionListener(new SelectionListener(){

		public void widgetSelected(SelectionEvent e) {
			//call cancel converting.
			if(cancelable!=null){
				ExplorerView.log.info("cancel called");
				cancelable.cancel();
			}
			closeWindow();
		}

		public void widgetDefaultSelected(SelectionEvent e) {
			
		}
		
	});
	cancel.setText(Messages.getString("explorer.cancel"));
	return composite;
}

protected void createButtonsForButtonBar(Composite parent) {
   
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
            true);
}

public SimpleFileList getFileList() {
	return fileList;
}

public void setFileList(SimpleFileList fileList) {
	this.fileList = fileList;
}

public MonitorCancelable getCancelable() {
	return cancelable;
}

public void setCancelable(MonitorCancelable cancelable) {
	this.cancelable = cancelable;
}
}
